package ext.acae2.baseline.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import wt.doc.WTDocument;
import wt.epm.EPMDocument;
import wt.log4j.LogR;
import wt.part.WTPart;
import wt.util.WTException;
import wt.util.WTProperties;
import wt.util.WTRuntimeException;
import wt.vc.baseline.ManagedBaseline;
import ext.acae2.util.VersionUtil;

public class DownloadCompareBaselineLatestData {

    private static final Logger LOGGER = LogR.getLogger(DownloadCompareBaselineLatestData.class.getName());
    
    public static void download(ManagedBaseline baseline, OutputStream outputStream) 
            throws WTRuntimeException, WTException, IOException {
        LOGGER.debug("> Start download");
        
        String wthome = (String) WTProperties.getLocalProperties().get("wt.home");
        
        File templateFile = new File(wthome + File.separator 
                + "codebase" + File.separator + "ext" + File.separator 
                + "acae2" + File.separator + "conf" + File.separator
                + "CompareBaselineLatestData.xls");
        
        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(templateFile));
        HSSFSheet sheet = workbook.getSheetAt(0);
        
        List<WTPart> partList = BaselineUtil.getBaselineParts(baseline);
        int rowNum = 3;
        for (WTPart part : partList) {
            HSSFRow row = sheet.createRow(rowNum);
            rowNum++;
            
            WTPart latest = (WTPart) VersionUtil.getLatestObject(part);
            row.createCell(0).setCellValue(part.getName());
            row.createCell(1).setCellValue(part.getNumber());
            row.createCell(2).setCellValue(
                    part.getVersionIdentifier().getValue() + "." 
                    + part.getIterationIdentifier().getValue());
            row.createCell(3).setCellValue(part.getLifeCycleState().getDisplay());
            row.createCell(4).setCellValue(
                    latest.getVersionIdentifier().getValue() + "." 
                    + latest.getIterationIdentifier().getValue());
            row.createCell(5).setCellValue(latest.getLifeCycleState().getDisplay());
            row.createCell(6).setCellValue(latest.getModifyTimestamp().toString());
        }
        
        List<WTDocument> docList = BaselineUtil.getBaselineDocuments(baseline);
        for (WTDocument doc : docList) {
            HSSFRow row = sheet.createRow(rowNum);
            rowNum++;
            
            WTDocument latest = (WTDocument) VersionUtil.getLatestObject(doc);
            row.createCell(0).setCellValue(doc.getName());
            row.createCell(1).setCellValue(doc.getNumber());
            row.createCell(2).setCellValue(
                    doc.getVersionIdentifier().getValue() + "." 
                    + doc.getIterationIdentifier().getValue());
            row.createCell(3).setCellValue(doc.getLifeCycleState().getDisplay());
            row.createCell(4).setCellValue(
                    latest.getVersionIdentifier().getValue() + "." 
                    + latest.getIterationIdentifier().getValue());
            row.createCell(5).setCellValue(latest.getLifeCycleState().getDisplay());
            row.createCell(6).setCellValue(latest.getModifyTimestamp().toString());
        }
        
        List<EPMDocument> epmList = BaselineUtil.getBaselineEPM(baseline);
        for (EPMDocument epm : epmList) {
            HSSFRow row = sheet.createRow(rowNum);
            rowNum++;
            
            EPMDocument latest = (EPMDocument) VersionUtil.getLatestObject(epm);
            row.createCell(0).setCellValue(epm.getName());
            row.createCell(1).setCellValue(epm.getNumber());
            row.createCell(2).setCellValue(
                    epm.getVersionIdentifier().getValue() + "." 
                    + epm.getIterationIdentifier().getValue());
            row.createCell(3).setCellValue(epm.getLifeCycleState().getDisplay());
            row.createCell(4).setCellValue(
                    latest.getVersionIdentifier().getValue() + "." 
                    + latest.getIterationIdentifier().getValue());
            row.createCell(5).setCellValue(latest.getLifeCycleState().getDisplay());
            row.createCell(6).setCellValue(latest.getModifyTimestamp().toString());
        }
        
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        
        LOGGER.debug("> End download");
    }
}
